package gui;

import gui.utils.TextFieldNumbers;
import gui.utils.UnsubscribeComboBoxModel;

import javax.swing.JComboBox;

public class PanelMultiPublish extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	private javax.swing.JButton buttonPublish;
	private javax.swing.JComboBox comboBoxTopicsList;
	private javax.swing.JLabel labelDescriptionMessage;
	private javax.swing.JLabel labelDescriptionPeriod;
	private javax.swing.JLabel labelDescriptionStep;
	private javax.swing.JLabel labelDescriptionTopicsList;
	private javax.swing.JTextField textFieldMessage;
	private gui.utils.TextFieldNumbers textFieldPeriod;
	private gui.utils.TextFieldNumbers textFieldStep;

	public PanelMultiPublish() {
		initComponents();
	}

	private void buttonPublishActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			ClientGuiLauncher.clientFrame.client.multipublish(textFieldMessage
					.getText(),
					comboBoxTopicsList.getSelectedItem().toString(), Integer
							.parseInt(textFieldPeriod.getText()) * 1000,
					Integer.parseInt(textFieldStep.getText()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}

	private void comboBoxTopicsListActionPerformed(
			java.awt.event.ActionEvent evt) {
		if (comboBoxTopicsList.getSelectedItem().equals(""))
			buttonPublish.setEnabled(false);
		else if (!textFieldMessage.getText().equals("")
				&& !textFieldPeriod.getText().equals("")
				&& !textFieldStep.getText().equals(""))
			buttonPublish.setEnabled(true);
	}

	protected void disableAll() {
		comboBoxTopicsList.setModel(new UnsubscribeComboBoxModel(
				ClientGuiLauncher.clientFrame.client.getSubscribedTopics()));
		textFieldMessage.setEditable(false);
		textFieldMessage.setText("");
		buttonPublish.setEnabled(false);
		textFieldPeriod.setEditable(false);
		textFieldPeriod.setText("");
		textFieldStep.setEditable(false);
		textFieldStep.setText("");
	}

	public JComboBox getComboBoxTopicsList() {
		return comboBoxTopicsList;
	}

	private void initComponents() {

		labelDescriptionTopicsList = new javax.swing.JLabel();
		comboBoxTopicsList = new javax.swing.JComboBox();
		labelDescriptionMessage = new javax.swing.JLabel();
		textFieldMessage = new javax.swing.JTextField();
		labelDescriptionPeriod = new javax.swing.JLabel();
		textFieldPeriod = new TextFieldNumbers(6);
		labelDescriptionStep = new javax.swing.JLabel();
		textFieldStep = new TextFieldNumbers(6);
		buttonPublish = new javax.swing.JButton();

		setPreferredSize(new java.awt.Dimension(700, 72));

		labelDescriptionTopicsList
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		labelDescriptionTopicsList
				.setText("Select the topic to which you want to publish");

		comboBoxTopicsList.setEditable(false);
		comboBoxTopicsList
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						comboBoxTopicsListActionPerformed(evt);
					}
				});

		labelDescriptionMessage
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		labelDescriptionMessage.setText("Message");

		textFieldMessage.setEditable(false);

		labelDescriptionPeriod.setText("Period [sec]");

		textFieldPeriod.setEditable(false);
		textFieldPeriod.addKeyListener(new java.awt.event.KeyAdapter() {
			@Override
			public void keyReleased(java.awt.event.KeyEvent evt) {
				textFieldPeriodKeyReleased(evt);
			}
		});

		labelDescriptionStep.setText("Step [msec]");

		textFieldStep.setEditable(false);
		textFieldStep.addKeyListener(new java.awt.event.KeyAdapter() {
			@Override
			public void keyReleased(java.awt.event.KeyEvent evt) {
				textFieldStepKeyReleased(evt);
			}
		});

		buttonPublish.setText("Publish");
		buttonPublish.setEnabled(false);
		buttonPublish.setMargin(new java.awt.Insets(2, 5, 2, 5));
		buttonPublish.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonPublishActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														comboBoxTopicsList,
														javax.swing.GroupLayout.Alignment.LEADING,
														0,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														labelDescriptionTopicsList,
														javax.swing.GroupLayout.Alignment.LEADING))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														textFieldMessage,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														159, Short.MAX_VALUE)
												.addComponent(
														labelDescriptionMessage,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														159, Short.MAX_VALUE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														textFieldPeriod,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														labelDescriptionPeriod,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														textFieldStep,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														labelDescriptionStep,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(buttonPublish,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										80,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(
																		buttonPublish,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		20,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		textFieldStep,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		textFieldPeriod,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(
																						labelDescriptionStep)
																				.addComponent(
																						labelDescriptionPeriod))
																.addGroup(
																		layout.createSequentialGroup()
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																								.addComponent(
																										labelDescriptionTopicsList)
																								.addComponent(
																										labelDescriptionMessage))
																				.addPreferredGap(
																						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																								.addComponent(
																										comboBoxTopicsList,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										20,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addComponent(
																										textFieldMessage,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.PREFERRED_SIZE)))))
								.addContainerGap(18, Short.MAX_VALUE)));
	}

	protected void setInitialComponentsState() {
		textFieldMessage.setEditable(true);
		textFieldPeriod.setEditable(true);
		textFieldStep.setEditable(true);
		textFieldMessage.setText("");
		buttonPublish.setEnabled(false);
		textFieldPeriod.setText("");
		textFieldStep.setText("");
		comboBoxTopicsList.setEditable(true);
		comboBoxTopicsList.setModel(new UnsubscribeComboBoxModel(
				ClientGuiLauncher.clientFrame.client.getSubscribedTopics()));
	}

	private void textFieldPeriodKeyReleased(java.awt.event.KeyEvent evt) {
		if (textFieldPeriod.getText().equals(""))
			buttonPublish.setEnabled(false);
		else if (!comboBoxTopicsList.getSelectedItem().equals("")
				&& !textFieldStep.getText().equals(""))
			buttonPublish.setEnabled(true);
	}

	private void textFieldStepKeyReleased(java.awt.event.KeyEvent evt) {
		if (textFieldStep.getText().equals(""))
			buttonPublish.setEnabled(false);
		else if (!comboBoxTopicsList.getSelectedItem().equals("")
				&& !textFieldPeriod.getText().equals(""))
			buttonPublish.setEnabled(true);
	}
}
